import {
  createRouter,
  createWebHistory,
  NavigationGuardNext,
  RouteLocationNormalized,
  RouteRecordRaw,
} from "vue-router";
import Login from "@/views/login/Login.vue";
import BaseTemplate from "@/components/layout/BaseTemplate.vue";
import userSystemRoute from "@/router/userSystemRoute";
import dataRoute from "@/router/dataRoute";
import salesRoute from "@/router/salesRoute";
import developerRoute from "@/router/developerRoute";
import deviceRoute from "@/router/deviceRoute";
import purchasingRoute from "@/router/purchasingRoute";
import technologyRoute from "@/router/technologyRoute";
import deliverRoute from "@/router/deliverRoute";
import inventoryRoute from "@/router/inventoryRoute";
import plansRoute from "@/router/planRoute";
import Annex from "@/views/annex/Annex.vue";
import processRoute from "@/router/processRoute";
import qualityRoute from "@/router/qualityRoute";

const baseRoutes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/pdf/test",
    name: "Pdf",
    component: Annex,
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/echarts",
    name: "echarts",
    component: () =>
      import(/* webpackChunkName: "echarts" */ "@/views/echarts/EchartsC.vue"),
  },
  {
    path: "/echartsReinforcement",
    name: "echartsReinforcement",
    component: () =>
      import(
        /* webpackChunkName: "echartsReinforcement" */ "@/views/echarts/EchartsReinforcement.vue"
      ),
  },
  {
    path: "/echartsProduction",
    name: "echartsProduction",
    component: () =>
      import(
        /* webpackChunkName: "echartsProduction" */ "@/views/echarts/EchartsProduction.vue"
      ),
  },
  {
    path: "/echartsLabelArea",
    name: "echartsLabelArea",
    component: () =>
      import(
        /* webpackChunkName: "echartsLabelArea" */ "@/views/echarts/EchartsLabelArea.vue"
      ),
  },
  {
    path: "/echartsYard",
    name: "echartsYard",
    component: () =>
      import(
        /* webpackChunkName: "echartsYard" */ "@/views/echarts/EchartsYard.vue"
      ),
  },
  {
    path: "/echartsWater",
    name: "echartsWater",
    component: () =>
      import(
        /* webpackChunkName: "echartsWater" */ "@/views/echarts/EchartsWaterAquarium.vue"
      ),
  },
  {
    path: "/test",
    name: "TestPage",
    component: () =>
      import(/* webpackChunkName: "testPage" */ "@/views/TestPage.vue"),
  },
  {
    path: "/test2",
    name: "TestPage2",
    component: () =>
      import(/* webpackChunkName: "testPage2" */ "@/views/TestPage2.vue"),
  },
  {
    path: "/",
    name: "BaseTemplate",
    component: BaseTemplate,
    children: [
      {
        path: "/home",
        name: "HomePage",
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/HomePage.vue"),
        meta: {
          title: "首页",
        },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: baseRoutes
    .concat(userSystemRoute)
    .concat(dataRoute)
    .concat(developerRoute)
    .concat(purchasingRoute)
    .concat(deviceRoute)
    .concat(technologyRoute)
    .concat(salesRoute)
    .concat(deliverRoute)
    .concat(inventoryRoute)
    .concat(qualityRoute)
    .concat(processRoute)
    .concat(plansRoute),
});

// 路由守卫, 拦截路由, 判断是否登录.
router.beforeEach(
  (
    to: RouteLocationNormalized,
    from: RouteLocationNormalized,
    next: NavigationGuardNext
  ) => {
    if (to.name !== "Login" && !sessionStorage.getItem("token"))
      next({ name: "Login" });
    else next();
  }
);

export default router;
